package com.wsc.springbootmultidevdemo.springMsg;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Files;
import java.util.UUID;

/**
 * PDF消息转换器
 */
public class PDFMessageConverter implements MessageConverter {

    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
        throw new MessageConversionException(" Message Convert Error");
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        System.err.println(" ----------- PDF Message Converter ---------- ");

        byte[] body = message.getBody();
        String fileName = UUID.randomUUID().toString();
        String path =  "/Users/weisicong/Downloads"+fileName+".pdf";
        File file = new File(path);
        try {
            Files.copy(new ByteArrayInputStream(body), file.toPath());
        }catch (Exception e){
            e.printStackTrace();
        }
        return file;
    }
}
